<?php
namespace app\manage\controller;
use app\manage\controller\Base;
use think\facade\Db;
use support\View;
use support\Request;
use app\model\LinkModel;

class LinkController extends Base
{
    public function index(Request $request)
    {
    	$link=new LinkModel();
    	$key=$request->get('key','');
        View::assign('key',$key);
		$link=$link->where('title','like','%'.$key.'%')
						->order('sort asc')
						->paginate(['list_rows'=>10,'query'=>request()->all()]);
        View::assign('link',$link);
        View::assign('count1', $link->total());
       	return view('link/index');
    }
	public function ajax(Request $request)
    {
    	$data=$request->post();
		$link=new LinkModel();
		if($data['type']=='link_del'){
			$id=$data['id'];
			$info=$link->destroy($id);
			if($info){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='link_sort'){
			$arrlength=count($data['id']);
			$ar=[];
			for($x=0;$x<$arrlength;$x++)
			{
			    $ar[$x]=['id'=>$data['id'][$x], 'sort'=>$data['sort'][$x]];
			}
			$info=$link->saveAll($ar);
			if($info){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='link_start'){
		    if($link->where('id',$data['id'])->update(['isopen'=>1])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
		if($data['type']=='link_stop'){
		    if($link->where('id',$data['id'])->update(['isopen'=>0])){
                return json(['code' => 1, 'message' => '操作成功']);
            } else {
                return json(['code' => 0, 'message' => '操作失败']);
            }
		}
        return json(['code' => 0, 'message' => '失败']);
    }
	public function add(Request $request)
    {
    	if(request()->method()=='POST'){
			$data=$request->post();
			$validate = new \app\manage\validate\LinkValidate;
			if(!$validate->check($data)){
				return view_e($validate->getError());
			}
            $file = $request->file('pic');
            if ($file && $file->isValid()  && $file->getSize() >0) {
                if(!in_array($file->getUploadExtension(), ['jpg','png','gif','webp','jpeg'])){return view_e('图片格式不正确');}
                if($file->getSize() > 5*1024*1024){return view_e('图片大小不能超过5M');}
                $path = '/pic/'.date("YmdHis").rand(10000,99999).session('uid', '0').'.'.$file->getUploadExtension();
                $file->move(public_path().$path);
                $data['pic']=$path;
            }
			$link=new LinkModel();
			$link->data($data);
			$res=$link->save();
			if($res){
				return view_s('添加成功',url('link/index',['st'=>1]));
			}else{
				return view_e('友情链接添加失败了');
			}
    	}
       return view('link/add');
    }
	public function edit(Request $request)
    {
    	$link = new LinkModel();
    	if(request()->method()=='POST'){
    		$data=$request->post();
			$validate = new \app\manage\validate\LinkValidate;
			if(!$validate->scene('edit')->check($data)){
				return view_e($validate->getError());
			}
            $file = $request->file('pic');
            if ($file && $file->isValid()  && $file->getSize() >0) {
                if(!in_array($file->getUploadExtension(), ['jpg','png','gif','webp','jpeg'])){return view_e('图片格式不正确');}
                if($file->getSize() > 5*1024*1024){return view_e('图片大小不能超过5M');}
                $path = '/pic/'.date("YmdHis").rand(10000,99999).session('uid', '0').'.'.$file->getUploadExtension();
                $file->move(public_path().$path);
                $data['pic']=$path;
            }
			$res=$link->update($data,['id' => $data['id']]);
			if($res){
				return view_s('修改成功',url('link/index',['st'=>1]));
			}else{
				return view_e('友情链接修改失败了');
			}
    	}
		$id=$request->input('id',0);
		$data= $link->find($id);
        View::assign('data',$data);
       return view('link/edit');
    }
}
